package com.his.benchwork.service.Imp;

import com.his.benchwork.dao.BenchWorkMapper;
import com.his.benchwork.entity.*;
import com.his.benchwork.service.BenchWorkEmpService;
import com.his.benchwork.service.BenchWorkService;
import com.his.benchwork.vo.BenchWorkEmpVo;
import com.his.benchwork.vo.BenchWorkVo;
import com.his.login.entity.DictEmployee;
import com.xiao.common.util.BeanUtil;
import com.xiao.common.util.WorkIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author jzy
 * @Date 2020/3/11 11:33
 * @Version 1.0
 */
@Service
public class BenchWorkServiceImpl implements BenchWorkService {

    @Resource
    private BenchWorkMapper benchWorkMapper;

    @Autowired
    private BenchWorkEmpService benchWorkEmpService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BenchWork qureyBenchWork(String benchCateId,String benchName,String hospId) {
        return benchWorkMapper.qureyBenchWork(benchCateId,benchName,hospId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<BenchWorkInfo> qureyBenchWorkList(DictEmployee dictEmployee,BenchWorkVo benchWorkVo) {
        String hospid = dictEmployee.getHOSPID();
        BenchWork benchWork = new BenchWork();
        benchWork.setHospid(hospid);
        if(benchWorkVo.getBenchcateid() != null){
            benchWork.setBenchcateid(benchWorkVo.getBenchcateid());
        }
        List<BenchWorks> benchWorksList= benchWorkMapper.benchworks_list(benchWork);
        List<BenchWorkInfo> benchWorkInfoList = new ArrayList<BenchWorkInfo>();
        for(int i =0 ; i<benchWorksList.size(); i++){
            BenchWorks benchWorks =benchWorksList.get(i);

            String benchid = benchWorks.getBenchid();
            BenchWorkEmpVo benchWorkEmpVo = new BenchWorkEmpVo();
            benchWorkEmpVo.setBenchid(benchid);
            //查询科室下的所有操作员
            List<BenchWorkEmps> benchWorkEmpList =benchWorkEmpService.selectBenchWorkEmpList(benchWorkEmpVo);

            BenchWorkInfo benchWorkInfo = new BenchWorkInfo();
                benchWorkInfo.setBenchid(benchWorks.getBenchid());
                benchWorkInfo.setHospid(benchWorks.getHospid());
                benchWorkInfo.setBenchcateid(benchWorks.getBenchcateid());
                benchWorkInfo.setBenchname(benchWorks.getBenchname());
                benchWorkInfo.setHospname(benchWorks.getHospname());
                benchWorkInfo.setBenchcatename(benchWorks.getBenchcatename());
                benchWorkInfo.setBenchWorkEmpsList(benchWorkEmpList);
            benchWorkInfoList.add(benchWorkInfo);
        }
        return benchWorkInfoList;
    }

    @Override
    public List<BenchWorks> qureyBenchWork_parm(DictEmployee dictEmployee) {
        return benchWorkMapper.benchworks_parm(dictEmployee.getHOSPID());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBenchWork(DictEmployee dictEmployee, BenchWorkVo benchWorkVo) {
        BenchWork benchWork = BeanUtil.copyBean(benchWorkVo,BenchWork.class);

        benchWork.setBenchid(WorkIdUtil.nextId());
        benchWork.setHospid(dictEmployee.getHOSPID());
        benchWork.setCreater_id(dictEmployee.getEMPID());
        benchWork.setCreate_time(new Date());

        benchWorkMapper.benchwork_save(benchWork);
    }

    @Override
    public void updateBenchWork(BenchWorkVo benchWorkVo) {
        BenchWork benchWork = BeanUtil.copyBean(benchWorkVo,BenchWork.class);
        benchWorkMapper.benchwork_update(benchWork);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByBenchId( BenchWorkVo benchWorkVo) {
        BenchWork benchWork = BeanUtil.copyBean(benchWorkVo,BenchWork.class);
        benchWorkMapper.deletebybenchid(benchWork.getBenchid());
    }
}
